package com.bw.kf.playvideo

import android.content.Context
import android.content.SharedPreferences

object UserManager {
    private const val PREF_NAME = "user_prefs"
    private const val KEY_USER_ID = "user_id"
    private const val KEY_USER_NAME = "user_name"
    private const val KEY_USER_AVATAR = "user_avatar"
    
    private lateinit var sharedPreferences: SharedPreferences
    
    fun init(context: Context) {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
    }
    
    fun getUserId(): Int {
        return sharedPreferences.getInt(KEY_USER_ID, 0)
    }
    
    fun getUserName(): String {
        return sharedPreferences.getString(KEY_USER_NAME, "匿名用户") ?: "匿名用户"
    }
    
    fun getUserAvatar(): String {
        return sharedPreferences.getString(KEY_USER_AVATAR, "") ?: ""
    }
    
    // 添加CommentsDialog需要的方法
    fun getCurrentUserId(): Int {
        return getUserId()
    }
    
    fun getCurrentUserName(): String {
        return getUserName()
    }
    
    fun getCurrentUserAvatar(): String {
        return getUserAvatar()
    }
    
    fun setUserInfo(userId: Int, userName: String, userAvatar: String) {
        sharedPreferences.edit()
            .putInt(KEY_USER_ID, userId)
            .putString(KEY_USER_NAME, userName)
            .putString(KEY_USER_AVATAR, userAvatar)
            .apply()
    }
    
    fun isLoggedIn(): Boolean {
        return getUserId() > 0
    }
    
    fun logout() {
        sharedPreferences.edit().clear().apply()
    }
}